package cn.ante.pet.controller;

import cn.ante.basic.annotation.PreAuthorize;
import cn.ante.basic.exception.BusinessException;
import cn.ante.basic.utils.LoginContext;
import cn.ante.org.domain.Employee;
import cn.ante.org.service.IEmployeeService;
import cn.ante.pet.domain.Pet;
import cn.ante.pet.query.PetQuery;
import cn.ante.pet.service.IPetService;
import cn.ante.pet.service.IPetTypeService;
import cn.ante.basic.utils.AjaxResult;
import cn.ante.basic.utils.PageList;
import cn.ante.user.domain.Logininfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/pet")
public class PetController {
    @Autowired
    public IPetService petService;

    @Autowired
    private IPetTypeService petTypeService;

    @Autowired
    private IEmployeeService employeeService;

    /**
     * 保存和修改公用的
     * @param pet  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    @ApiOperation(value = "保存/修改宠物信息")
    @PreAuthorize(name ="保存/修改宠物信息",sn="pet:addOrUpdate")
    public AjaxResult addOrUpdate(@RequestBody Pet pet, HttpServletRequest request){
        Logininfo logininfo = LoginContext.getLoginifo(request);
        Employee employee = employeeService.loadByLoginifoId(logininfo.getId());
        try {
            if( pet.getId()!=null) {
                petService.update(pet);
            } else{
                pet.setShopId(employee.getShopId());
                petService.add(pet);}
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除对象失败！");
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    @ApiOperation(value = "根据id删除宠物")
    @PreAuthorize(name ="根据id删除宠物",sn="pet:delete")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            petService.remove(id);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除对象失败！");
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询宠物")
    @PreAuthorize(name ="根据id查询宠物",sn="pet:get")
    public Pet get(@PathVariable("id")Long id)
    {
        return petService.loadById(id);
    }

    @GetMapping("/detail/{id}")
    @ApiOperation(value = "根据id查询宠物详情")
    public Pet getDetail(@PathVariable("id")Long id) {
        Pet pet = petService.loadByIdDetail(id);
        System.out.println(pet);
        return petService.loadByIdDetail(id);
    }
    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Pet> list(){

        return petService.loadAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    @ApiOperation(value = "分页展示宠物")
    @PreAuthorize(name ="分页展示宠物",sn="pet:list")
    public PageList<Pet> json(@RequestBody PetQuery query) {
        PageList<Pet> all = petService.findAll(query);
        return all;
    }

    @PostMapping("/offsale")
    public AjaxResult offsale(@RequestBody Long[] ids){
        try {
            petService.offSale(ids);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除对象失败！");
        }
    }

    @PostMapping("/onsale")
    public AjaxResult onsale(@RequestBody Long[] ids){
        try {
            petService.onSale(ids);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除对象失败！");
        }
    }

}
